<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Wilead\ApiBundle\Controller;

use FOS\RestBundle\Controller\Annotations\RequestParam;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\View\RouteRedirectView;

/**
 * Description of UserSkillController
 *
 * @author jack
 */
class UserSkillController extends ApiController {

    /**
     * @ApiDoc(
     *      description="添加用户专业技能"
     * )
     * @RequestParam(name="User", requirements="integer", description="用户ID")
     * @RequestParam(name="skill", requirements="string", description="用户技能")
     */
    public function addAction() {
        $request = $this->getRequest()->request;

        $entity = new \Wilead\IArtistBundle\Entity\UserSkill;
        $entity->setUser($this->getEntityManager()->getReference('Wilead\IArtistBundle\Entity\User', $request->get('User')));
        $entity->setSkill($request->get('skill'));

        $em = $this->getEntityManager();
        $em->persist($entity);
        $em->flush();
        return RouteRedirectView::create('wilead_api_user_get', array('id' => $entity->getUser()->getId()), 200);
    }

    /**
     * @ApiDoc(
     *      description="删除用户专业技能"
     * )
     * @RequestParam(name="id", requirements="integer", description="用户专业技能ID")
     */
    public function deleteAction(\Symfony\Component\HttpFoundation\Request $request) {
        if ($request->request->has('id')) {
            $entity = $this->getEntityManager()->getRepository('WileadIArtistBundle:UserSkill')->find($request->request->get('id'));
            if (!$entity)
                throw $this->createNotFoundException();

            $userId = $entity->getUser()->getId();
            $em = $this->getEntityManager();
            $em->remove($entity);
            $em->flush();

            return RouteRedirectView::create('wilead_api_user_get', array('id' => $userId), 200);
        } else {
            throw $this->createNotFoundException();
        }
    }

}
?>
